package com.aplink.generic.handler;

import android.os.Handler;
import android.os.Message;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.constant.type.DialogStatus;
import com.aplink.generic.controller.GenericApplication;
import com.aplink.generic.pojo.MasterConfig;
import com.aplink.generic.sharepreference.GenericSharePreference;

public class EventHandler extends Handler {
    private final GenericFragment mGenericFragment;

    public EventHandler(final GenericFragment genericFragment) {
        this.mGenericFragment = genericFragment;
    }

    private void handleConfirmDialogGenericHandler(
            final DialogStatus alertDialogStatus, final ActionType actionType,
            final Message message) {
        this.mGenericFragment.getGenericHandler().sendAction(actionType);
    }

    private void handleConfirmDialogWebserviceHandler(
            final ActionType actionType, final Message message) {
        if (GenericSharePreference.getInstance().isOfflineMode()
                && !actionType.equals(ActionType.UPLOAD)
                && !actionType.equals(ActionType.DOWNLOAD)
                && !actionType.equals(ActionType.DOWNLOAD_ALL)) {
            this.mGenericFragment.getDatabaseHandler().sendAction(actionType);
        } else {
            if (GenericApplication.getInstance().isNetworkAvailable()) {
                this.mGenericFragment.getWebserviceHandler().sendAction(
                        actionType);
            } else {
                this.mGenericFragment.getAlertDialog().show(actionType,
                        DialogStatus.NO_INTERNET_ACCESS);
            }
        }
    }

    @Override
    public void handleMessage(final Message msg) {
        final ActionType actionType = ActionType.get(msg.what);
        if (actionType.equals(ActionType.LOGIN)) {
            final MasterConfig masterConfig = GenericApplication.getInstance()
                    .getMasterConfig();
            if (masterConfig.isDemo()) {
                if (masterConfig.isHasOfflineMode()) {
                    sendEmptyMessage(ActionType.LOGIN_OFFLINE.getCode());
                }
            } else {
                if (GenericSharePreference.getInstance().isFirstStart()) {
                    if (GenericApplication.getInstance().isNetworkAvailable()) {
                        this.mGenericFragment.getWebserviceHandler()
                                .sendAction(ActionType.LOGIN);
                    } else {
                        this.mGenericFragment.getAlertDialog().show(actionType,
                                DialogStatus.NO_INTERNET_ACCESS);
                    }
                } else {
                    sendEmptyMessage(ActionType.LOGIN_OFFLINE.getCode());
                }
            }
        } else if (actionType.equals(ActionType.LOGIN_OFFLINE)) {
            this.mGenericFragment.getDatabaseHandler().sendAction(
                    ActionType.LOGIN_OFFLINE);
        } else if (actionType.equals(ActionType.LOGOUT)) {
            handleConfirmDialogGenericHandler(DialogStatus.CONFIRM,
                    ActionType.LOGOUT, msg);
        } else if (actionType.equals(ActionType.RESET_APPLICATION)) {
            handleConfirmDialogGenericHandler(DialogStatus.CONFIRM,
                    ActionType.RESET_APPLICATION, msg);
        } else if (actionType.equals(ActionType.DOWNLOAD)) {
            if (GenericApplication.getInstance().isNetworkAvailable()) {
                handleConfirmDialogWebserviceHandler(ActionType.DOWNLOAD, msg);
            } else {
                this.mGenericFragment.getAlertDialog().show(actionType,
                        DialogStatus.NO_INTERNET_ACCESS);
            }
        } else if (actionType.equals(ActionType.DOWNLOAD_ALL)) {
            if (GenericApplication.getInstance().isNetworkAvailable()) {
                handleConfirmDialogWebserviceHandler(ActionType.DOWNLOAD_ALL,
                        msg);
            } else {
                this.mGenericFragment.getAlertDialog().show(actionType,
                        DialogStatus.NO_INTERNET_ACCESS);
            }
        } else if (actionType.equals(ActionType.UPLOAD)) {
            if (GenericApplication.getInstance().isNetworkAvailable()) {
                handleConfirmDialogWebserviceHandler(ActionType.UPLOAD, msg);
            } else {
                this.mGenericFragment.getAlertDialog().show(actionType,
                        DialogStatus.NO_INTERNET_ACCESS);
            }
        } else if (actionType.equals(ActionType.NEXT_PAGE)) {
            this.mGenericFragment.getGenericHandler().sendAction(
                    ActionType.NEXT_PAGE);
        } else if (actionType.equals(ActionType.INSERT)) {
            handleConfirmDialogWebserviceHandler(actionType, msg);
        } else if (actionType.equals(ActionType.DELETE)) {
            handleConfirmDialogWebserviceHandler(actionType, msg);
        } else if (actionType.equals(ActionType.UPDATE)) {
            handleConfirmDialogWebserviceHandler(actionType, msg);
        } else if (actionType.equals(ActionType.SAVE)) {
            handleConfirmDialogWebserviceHandler(actionType, msg);
        } else if (actionType.equals(ActionType.REFRESH_PAGE)) {
            this.mGenericFragment.getGenericHandler().sendAction(
                    ActionType.REFRESH_PAGE);
        } else if (actionType.equals(ActionType.CLOSE)) {
            this.mGenericFragment.getGenericHandler().sendAction(
                    ActionType.CLOSE);
        } else if (actionType.equals(ActionType.FORGOT_PASSWORD)) {
            this.mGenericFragment.getWebserviceHandler().sendAction(
                    ActionType.FORGOT_PASSWORD);
        } else if (actionType.equals(ActionType.CLEAR_FORM)) {
            this.mGenericFragment.clearAll();
        }
    }

    public void sendAction(final ActionType actionType) {
        sendEmptyMessage(actionType.getCode());
    }

    public void sendMessage(final ActionType actionType, final Object object) {
        final Message message = Message.obtain(this, actionType.getCode(),
                object);
        message.sendToTarget();
    }
}
